<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <Form
      ref="formCom"
      :validation-schema="mySchema"
      v-slot="{ errors }"
      autocomplete="off"
      class="xtx-form"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            :class="{ err: errors.mobile }"
            v-model="form.mobile"
            name="mobile"
            class="input"
            type="text"
            placeholder="绑定的手机号"
          />
          <!-- <input class="input" type="text" placeholder="绑定的手机号" /> -->
        </div>
        <div class="error" v-if="errors.mobile">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            :class="{ err: errors.code }"
            v-model="form.code"
            name="code"
            class="input"
            type="text"
            placeholder="短信验证码"
          />
          <!-- <input class="input" type="text" placeholder="短信验证码" /> -->
          <span class="code" @click="send">{{
            countTime === 0 ? "发送验证码" : countTime + "s"
          }}</span>
        </div>
        <div class="error" v-if="errors.code">{{ errors.code }}</div>
      </div>
    </Form>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { ref, reactive } from 'vue'
import { Form, Field } from 'vee-validate'
import schema from '@/utils/validate-scheme'
import { useCountDown } from '@/compositions/index.js'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { $Message } from '@/components/Message/index.js'
export default {
  name: 'CallbackBind',
  components: {
    Form, Field
  },
  props: {
    openId: {
      type: String
    }
  },
  setup (props) {
    // 获取qq账号的用户名头像
    // 核心方法 ：QC.api('get_user_info').success(( res )=>{})
    // 调用前也必须先调用 check方法 保证已经登录了
    // 名称和头像
    const nickname = ref('')
    const avatar = ref('')
    const store = useStore()
    if (QC.Login.check()) {
      QC.api('get_user_info').success(res => {
        // res拿到当前登录所有信息
        avatar.value = res.data.figureurl_qq_1
        nickname.value = res.data.nickname
      })
    }
    // 表单数据对象
    const form = reactive({
      mobile: null,
      code: null
    })
    // 校验规则对象
    const mySchema = {
      mobile: schema.mobile,
      code: schema.code
    }
    // 发送验证码，60s倒计时
    const { countTime, start } = useCountDown(60)
    function send () {
      // 开启之前
      // 验证手机号是否ok
      // 调用发送验证码接口
      if (schema.mobile(form.mobile) === true) {
        // 只有countTime为0时才能调用接口
        if (countTime.value === 0) {
          // 调用接口成功后开启倒计时
          userQQBindCode(form.mobile).then(res => {
            start() // 开始倒计时
          })
        }
      }
    }
    // 立刻绑定
    const formCom = ref(null)
    const router = useRouter()
    async function bind () {
      // 兜底校验
      const res = await formCom.value.validate()
      if (res) {
        userQQBindLogin({ unionId: props.openId, mobile: form.mobile, code: form.code }).then((res) => {
          console.log(res)
          // 把返回的数据通过mutation存入vuex
          store.commit('user/setStateUser', res.result)
          // 2.跳转首页
          router.push('/')
          // 3.提示绑定成功
          $Message({ type: 'success', text: '绑定成功' })
        })
      }
    }
    return {
      avatar, nickname, form, mySchema, countTime, send, bind
    }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
